﻿using System.IO;
using System.Runtime.Serialization;
using System.Text;
using System.Xml.Serialization;
using System.Collections.Generic;
using System;
using System.Reflection;

namespace NObsidian.Containers
{
    [DataContract]
    public class ContainerBase
    {
        [DataMember(Name = "id", EmitDefaultValue=false)]
        [ObsidianReadOnly]
        public string Id { get; set; }

        [DataMember(Name = "name", EmitDefaultValue = false)]
        public string Name { get; set; }

        [DataMember(Name = "slug", EmitDefaultValue = false)]
        [ObsidianReadOnly]
        public string Slug { get; set; }

        [DataMember(Name = "created_at", EmitDefaultValue = false)]
        [ObsidianReadOnly]
        public string CreatedAt { get; set; }

        [DataMember(Name = "updated_at", EmitDefaultValue = false)]
        [ObsidianReadOnly]
        public string UpdatedAt { get; set; }

        /// <summary>
        /// Returns an XML View of the object
        /// The objective of this is make easier to debug and test the API
        /// </summary>
        /// <returns>XML string with the serialized object</returns>
        public override string ToString()
        {
            XmlSerializer  ser;
            ser = new XmlSerializer(this.GetType());

            MemoryStream ms = new MemoryStream();
            ser.Serialize(ms, this);
            
            return Encoding.Default.GetString(ms.ToArray());
        }

        public static List<string> GetContainerPropertyList(Type type)
        {
            List<string> list = new List<string>();

            foreach (PropertyInfo pi in type.GetProperties())
            {
                if (pi.GetCustomAttributes(typeof(DataMemberAttribute), true).Length > 0)
                {
                    list.Add(pi.Name);
                }
            }

            list.Sort();

            return list;
        }

        #region Original Values

        private object _clone;

        public void PopulateOriginalValues()
        {
            _clone = this.MemberwiseClone();
        }

        public T GetMinimumUpdateObject<T>() where T : new()
        {
            T newObject = new T();

            foreach (PropertyInfo pi in this.GetType().GetProperties())
            {
                if (pi.GetCustomAttributes(typeof(DataMemberAttribute), true).Length > 0
                    && pi.GetCustomAttributes(typeof(ObsidianReadOnlyAttribute), true).Length == 0)
                {
                    if (_clone == null || pi.GetValue(this, null) != pi.GetValue(_clone, null))
                    {
                        pi.SetValue(newObject, pi.GetValue(this, null), null);
                    }
                }
            }

            return newObject;
        }

        #endregion

        public object GetCopy()
        {
            object copy;
            copy = this.MemberwiseClone();
            ((ContainerBase)copy)._clone = null;

            return copy;
        }

    }
}
